package com.soa.ws2http.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.springframework.util.StringUtils;

public class WebserviceExcuteUtil {
	public static String getWSJsonValue(String url, String namespace, String method, Map<String, String> params)
			throws AxisFault {
		ServiceClient sc = new ServiceClient();
		sc.setTargetEPR(new EndpointReference(url));
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMElement methodEl = fac.createOMElement(method, fac.createOMNamespace(namespace, method));
		if (params != null) {
			for (String key : params.keySet()) {
				OMElement value = fac.createOMElement(new QName(key));
				value.setText(params.get(key));
				methodEl.addChild(value);
			}
		}
		OMElement resultEl = sc.sendReceive(methodEl);
		Iterator<?> iterator = resultEl.getChildElements();
		while (iterator.hasNext()) {
			OMElement om = (OMElement) iterator.next();
			return om.getText();
		}
		return null;
	}

	public static String getWSJson(String url, String namespace, String method, String action,
			Map<String, String> params) throws AxisFault, XMLStreamException {
		Options options = new Options();
		EndpointReference targetEPR = new EndpointReference(url);
		options.setTo(targetEPR);
		if (!StringUtils.isEmpty(action)) {
			options.setAction(action);
		}
		ServiceClient sc = new ServiceClient();
		sc.setOptions(options);
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace(namespace, method);
		OMElement methodEl = fac.createOMElement(method, omNs);
		if (params != null) {
			for (String key : params.keySet()) {
				OMElement value = null;
				if (!StringUtils.isEmpty(action)) {
					value = fac.createOMElement(key, omNs);
				} else {
					value = fac.createOMElement(new QName(key));
				}
				value.setText(params.get(key));
				methodEl.addChild(value);
			}
		}
		methodEl.build();
		OMElement resultEl = sc.sendReceive(methodEl);
		// Iterator<?> iterator = resultEl.getChildElements();
		// while (iterator.hasNext()) {
		// OMElement om = (OMElement) iterator.next();
		// Iterator<?> iterator2 = om.getChildElements();
		// while (iterator2.hasNext()) {
		// OMElement om2 = (OMElement) iterator2.next();
		// return om2.toStringWithConsume();
		// }
		// }
		return resultEl.toString();
	}

	public static String getWSJson(String url, String namespace, String method, Map<String, String> params)
			throws AxisFault, XMLStreamException {
		return getWSJson(url, namespace, method, namespace+method, params);
	}
	public static void main(String[] args) {
		String url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
		String method="getSupportCityString";
		String namespace="http://WebXml.com.cn/";
		Map<String, String> params = new HashMap<String,String>();
		params.put("theRegionCode", "北京");
		
		try {
			String result= getWSJson(url,namespace,method,params);
			System.out.println(result);
			System.out.println(XmlJsonUtil.xml2Json(result));
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
